package com.lanou3g.k12api.web.controller;

import com.lanou3g.k12api.entity.Carousel;
import com.lanou3g.k12api.mapper.CarouselMapper;
import com.lanou3g.k12api.utils.K12Result;
import com.lanou3g.k12api.utils.K12ResultUtils;
import com.lanou3g.k12api.utils.RandomEntityUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Api(description = "轮播图管理")
@RestController
public class CarouselController {

    @Autowired
    private CarouselMapper carouselMapper;

    @ApiOperation(value = "新增轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "轮播图ID（新增接口不可传ID）", paramType = "form"),
            @ApiImplicitParam(name = "title", value = "轮播图标题", required = true, example = "开学季", paramType = "form"),
            @ApiImplicitParam(name = "image", value = "轮播图图片地址", required = true, example = "https://wx3.sinaimg" +
                    ".cn/large/007dOZvply1g2curtd7flj31hc0mz15p.jpg", paramType = "form"),
            @ApiImplicitParam(name = "description", value = "描述", example = "喜迎开学季！XXX课程大减价，只要998，限时1天！",
                    paramType = "form"),
            @ApiImplicitParam(name = "link", value = "链接", example = "/course/1", paramType = "form"),
            @ApiImplicitParam(name = "sort", value = "轮播图排序值（升序）", paramType = "form", required = true, example = "1"),
            @ApiImplicitParam(name = "createTime", value = "创建时间（不用传）", paramType = "form"),
            @ApiImplicitParam(name = "status", value = "状态（0 代表正常，1 代表停用）", required = true, defaultValue = "0",
                    allowableValues = "0,1", paramType = "form")
    })
    @PostMapping("/carousel")
    public K12Result create(Carousel carousel) {
        System.out.println(carousel);
        return K12ResultUtils.ok();
    }

    @ApiOperation(value = "通过ID删除轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要删除的ID", example = "1")
    })
    @DeleteMapping("/carousel/{id}")
    public K12Result delete(@PathVariable Long id) {
        System.out.println(id);
        return K12ResultUtils.ok();
    }

    @ApiOperation(value = "通过ID批量删除轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "要删除的ID（逗号分隔）", example = "1,2,3")
    })
    @DeleteMapping("/carousels/{ids}")
    public K12Result delete(@PathVariable Long[] ids) {
        System.out.println(Arrays.toString(ids));
        return K12ResultUtils.ok();
    }

    @ApiOperation(value = "通过ID修改轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "轮播图ID", paramType = "form"),
            @ApiImplicitParam(name = "title", value = "轮播图标题", required = true, example = "开学季", paramType = "form"),
            @ApiImplicitParam(name = "image", value = "轮播图图片地址", required = true, example = "https://wx3.sinaimg" +
                    ".cn/large/007dOZvply1g2curtd7flj31hc0mz15p.jpg", paramType = "form"),
            @ApiImplicitParam(name = "description", value = "描述", example = "喜迎开学季！XXX课程大减价，只要998，限时1天！",
                    paramType = "form"),
            @ApiImplicitParam(name = "link", value = "链接", example = "/course/1", paramType = "form"),
            @ApiImplicitParam(name = "sort", value = "轮播图排序值（升序）", paramType = "form", required = true, example = "1"),
            @ApiImplicitParam(name = "createTime", value = "创建时间（不用传）", paramType = "form"),
            @ApiImplicitParam(name = "status", value = "状态（0 代表正常，1 代表停用）", required = true, defaultValue = "0",
                    allowableValues = "0,1", paramType = "form")
    })
    @PutMapping("/carousel/{id}")
    public K12Result update(@PathVariable Long id, Carousel carousel) {
        System.out.println(id);
        System.out.println(carousel.getId());
        return K12ResultUtils.ok();
    }

    @ApiOperation(value = "通过ID获取轮播图")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要获取的轮播图的ID", example = "1")
    })
    @GetMapping("/carousel/{id}")
    public K12Result findById(@PathVariable Long id) {
        Carousel carousel = RandomEntityUtils.random(Carousel.class);
        carousel.setId(id);
        return K12ResultUtils.ok(carousel);
    }

    @ApiOperation(value = "获取轮播图列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", defaultValue = "1"),
            @ApiImplicitParam(name = "size", value = "每页数据量（建议不传）", defaultValue = "30"),
            @ApiImplicitParam(name = "keyword", value = "查询关键词"),
    })
    @GetMapping("/carousels")
    public K12Result list(@RequestParam(defaultValue = "1") int page,
                          @RequestParam(defaultValue = "30") int size,
                          @RequestParam(defaultValue = "") String keyword) {
        System.out.println(page + "\t" + size + "\t" + keyword);
        List<Carousel> carousels = RandomEntityUtils.random(Carousel.class, size);
        return K12ResultUtils.ok(carousels);
    }

    @GetMapping("/gen")
    public void gen() {
        List<Carousel> list = RandomEntityUtils.random(Carousel.class, 100000);
        for (Carousel carousel : list) {
            carouselMapper.insert(carousel);
        }
    }

}
